﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.DataProtection;
using Microsoft.Owin.Security.Facebook;
using Microsoft.Owin.Security.Google;
using Owin;
using System;

namespace TheCup
{
    public partial class Startup
    {
        // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context and user manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

            // Enable the application to use a cookie to store information for the signed in user
            // and to use a cookie to temporarily store information about a user logging in with a third party login provider
            // Configure the sign in cookie
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });
            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Uncomment the following lines to enable logging in with third party login providers
            //app.UseMicrosoftAccountAuthentication(
            //    clientId: "",
            //    clientSecret: "");

            //app.UseTwitterAuthentication(
            //   consumerKey: "",
            //   consumerSecret: "");
            
            app.UseFacebookAuthentication(new FacebookAuthenticationOptions { 
                AppId = "474006262710091",
                AppSecret = "b6080901b1b31d7554139c394b2ed9e8",
                CallbackPath = new PathString("/facebook-oauth-redirect")
            });
            
            app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
            {
                // production
                ClientId = "104355804711-elu8huqqpke49fcjnum71ll57ida3544.apps.googleusercontent.com",
                ClientSecret = "BbCaowYQKyRzGD0BGIwjgcsj",
                CallbackPath = new PathString("/oauth2callback")

                // local
                //ClientId = "104355804711-ccf078ni75cd1q3jhie0sgu4rf9d1qhj.apps.googleusercontent.com",
                //ClientSecret = "mRWyvlLne-UyQMtG4VdLeygD",
                //CallbackPath = new PathString("/oauth2callback")
            });
        }
    }
}